<template>
    <div class="stats-panel">
        <div class="stats-panel-body">
            <div class="stats-btn-group">
                <button type="button" :class="{'stats-checked': chartType == 'VIEWS_QUANTITY'}" @click="changeChartType('VIEWS_QUANTITY')">{{$t('portal.dataBoard.pageView')}}</button>
                <button type="button" :class="{'stats-checked': chartType == 'LOGIN_QUANTITY'}" @click="changeChartType('LOGIN_QUANTITY')">{{$t('portal.dataBoard.loginNum')}}</button>
                <button type="button" :class="{'stats-checked': chartType == 'EVENT_CREATE'}" @click="changeChartType('EVENT_CREATE')">{{$t('portal.dataBoard.addEvent')}}</button>
            </div>
            <div class="category-chart-wrap">
                <div id="pageviewsLine" ref="pageviewsLine" class="pie-item-container"></div>
            </div>
        </div>
    </div>
</template>

<script>
import echarts from 'echarts';
let currCharts = null;
export default {
    props: {
        list: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            chartType: 'VIEWS_QUANTITY',
        }
    },
    watch: {
        list: function(newValue, oldValue){
            if (newValue) {
                this.updateCharts(newValue);
            }
        },
    },
    mounted() {
        this.$nextTick(() => {
            this.initECharts()
        })
    },
    beforeDestroy() {
        currCharts = null;
    },
    methods: {
        changeChartType(type) {
            this.chartType = type
            this.$emit('change', type)
        },
        // 初始化图表
        initECharts() {
            currCharts = echarts.init(document.getElementById('pageviewsLine')); 
        },
        getEChartsOption(data) {
            const chartsData = data;
            const option = {
                xAxis: {
                    type: 'category',
                    data: chartsData.map((item) => {return item.statisticsTime}),
                    axisLabel: {
                        rotate: 30
                    }
                },
                yAxis: {
                    type: 'value'
                },
                tooltip: {
                    trigger: 'axis'
                },
                series: [
                    {
                        data: chartsData.map((item) => {return item.count}),
                        type: 'line',
                        itemStyle: {
                            normal: {
                                color: '#5B8FF9',
                                lineStyle: {
                                    color: '#5B8FF9',
                                }
                            }
                        }
                    }
                ]              
            };
            return option;
        },
        getEchartsData(data) {
            return data || [];
        },
        updateCharts(update) {
            let data = this.getEchartsData(update);
            let option = this.getEChartsOption(data);
            currCharts.setOption(option);
        }
    },
};
</script>

<style lang="scss" scoped>
.stats-panel{
    margin-top: 0;
}
.stats-btn-group{
    margin-left: 16px;
    padding-top: 0;
}
.pie-item-container{
    height: 450px;
}
</style>
